#include "Collision.h"
#include "Collideable.h"
#include "DebugLog.h"
#include "SlopeTile.h"
#include <vector>

bool Collision::intersect(sf::FloatRect lhs, sf::FloatRect rhs)
{
	if ( (lhs.left + lhs.width) <= rhs.left) return false;
	if (lhs.left >= (rhs.left + rhs.width) ) return false;
	if ( (lhs.top + lhs.height) <= rhs.top ) return false;
	if ( lhs.top >= (rhs.top + rhs.height) ) return false;

	return true;
}

bool Collision::slopeCollision(Entity* entity, Collideable* collideable, Tile* tile, const sf::FloatRect& collision)
{
	SlopeTile* slopeTile = dynamic_cast<SlopeTile*>(tile);
	if (slopeTile != nullptr)
	{
		std::vector<sf::FloatRect>& collisionData = slopeTile->getCollisionData();
		for (unsigned int i = 0; i < collisionData.size(); i++)
		{
			if (Collision::intersect(collision, collisionData[i]))
			{
				entity->_position.y = collisionData[i].top - collideable->collisionBox().height * 0.5f;
				return true;
			}
		}
	}
	else
		Debug::log("Collision::slopeCollision WARNING: Failed to dynamic_cast Tile* tile");
	return false;
}